package com.ocm.core.domain;

import java.util.ArrayList;
import java.util.List;

import com.ocm.core.domain.Permission;

public enum AccessMask {

   VIEWPROPERTIES(new Permission[]{Permission.VIEWPROPERTIES,Permission.VIEWPERMISSION}),
   LINKPROPERTIES(new Permission[]{Permission.VIEWPROPERTIES,Permission.VIEWPERMISSION,Permission.LINK}),
   MODIFYPROPERTIES(new Permission[]{Permission.VIEWPROPERTIES,Permission.VIEWPERMISSION,Permission.CREATEINSTANCE,Permission.MODIFYPROPERTIES,Permission.LINK}),
   MODIFYPROPERTIES_PERMISSION(new Permission[]{Permission.VIEWPROPERTIES,Permission.VIEWPERMISSION,Permission.CREATEINSTANCE,Permission.MODIFYPROPERTIES,Permission.MODIFYPERMISSON,Permission.LINK}),
   FULLPERMISSION(new Permission[]{Permission.VIEWPROPERTIES,Permission.VIEWPERMISSION,Permission.CREATEINSTANCE,Permission.MODIFYPROPERTIES,Permission.DELETE,Permission.MODIFYPERMISSON, Permission.LINK});

   private Permission[] permissions;
   
   private AccessMask(Permission[] permissions) {
      this.permissions = permissions;
   }

   public Permission[] getPermissions() {
      return permissions;
   }

   public Permission[] getRestPermissions(Permission[] pers) {
      List<Permission> expectedPermission = new ArrayList<Permission>();
      for (Permission permission : this.permissions){
         boolean hasExist = false;
         for (Permission existPermission : pers){
            if (existPermission == permission){
               hasExist = true;
               break;
            }
         }
         if (hasExist==false){
            expectedPermission.add(permission);
         }
      }
      Permission[] result = expectedPermission.toArray(new Permission[]{});
      return result;
   }

   public void setPermissions(Permission[] permissions) {
      this.permissions = permissions;
   }

   public int getAccessMask() {
      int accessMask = 0;
      for (int index = 0;index<permissions.length; index++){
         accessMask += permissions[index].getPermission();
      }
      return accessMask;
   }
}
